package com.aggron.common;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * spring上下文
 * @author  liuhuan on 2018/6/2.
 */
@Component("applcationContextHelper")
public class ApplcationContextHelper implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ApplcationContextHelper.applicationContext=applicationContext;
    }

    public static <T> T getBean(Class<T> tClass){
        if(applicationContext==null){
            return null;
        }
        return applicationContext.getBean(tClass);
    }
    public static <T> T getBean(String name,Class<T> tclass){
        if(applicationContext==null){
            return null;
        }
        return applicationContext.getBean(name,tclass);
    }
}
